<?php

  function code2openid() {
    $appid = 'wxbffa43f56a3d96bf';
    $sessionKey = 'bcdbd11860e0e74477d6edcf1946d657';
    $code = $_POST['code'];
    $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$sessionKey}&js_code={$code}&grant_type=authorization_code";
    $resp = file_get_contents($url);
    $robot = json_decode($resp);
    return $robot -> openid;
  }

  function getSwiper() {
    $sql = "SELECT * FROM `swiper` where `validity` = 1 order by id desc";
    $query = mysql_query($sql);
    while ($rs = mysql_fetch_array($query)) $swiper[] = array(
      'id' => $rs['id'],
      'image' => $rs['image'],
      'url' => $rs['url']
    );
    mysql_close($link);
    print_r(json_encode($swiper));
  }

  function login() {
    $openid = $_POST['openid'];

    $sql = "SELECT * FROM `user` where `openid`='$openid'";
    $query = mysql_query($sql);
    while ($rs = mysql_fetch_array($query)) $user = array(
      'id' => $rs['id'],
      'name' => $rs['name'],
      'nick' => $rs['nick'],
      'gender' => $rs['gender'],
      'phone' => $rs['phone'],
      'contact' => $rs['contact'],
      'validity' => $rs['validity'],
      'last_login' => $rs['last_login'],
      'date' => $rs['date'],
      'city' => $rs['city'],
      'language' => $rs['language'],
      'introduction' => $rs['introduction'],
      'avatar' => $rs['avatar'],
    );
    print_r(json_encode($user));
  }
  

  function register() {
    $openid = $_POST['openid'];
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $nick = $_POST['nick'];
    $avatar = $_POST['avatar'];
    $city = $_POST['city'];
    $gender = $_POST['gender'];
    $language = $_POST['language'];

    $sql = "INSERT INTO `user` (`name`, `openid`, `phone`, `nick`, `avatar`, `gender`, `city`, `language`) VALUES ('{$name}', '{$openid}', '{$phone}', '{$nick}', '{$avatar}', '{$gender}', '{$city}', '{$language}');";
    $query = mysql_query($sql);
    $sql = "SELECT * FROM `user` where `openid` = '$openid'";
    $query = mysql_query($sql);
    while ($rs = mysql_fetch_array($query)) $user = array(
      'id' => $rs['id'],
      'name' => $rs['name'],
      'nick' => $rs['nick'],
      'gender' => $rs['gender'],
      'phone' => $rs['phone'],
      'contact' => $rs['contact'],
      'validity' => $rs['validity'],
      'last_login' => $rs['last_login'],
      'date' => $rs['date'],
      'city' => $rs['city'],
      'language' => $rs['language'],
      'introduction' => $rs['introduction'],
      'avatar' => $rs['avatar'],
    );
    print_r(json_encode($sql));  
    mysql_close($link);
  }


  function updateUser() {
    $id = $_POST['id'];
    $avatar = $_POST['avatar'];
    $last_login = $_POST['last_login'];
    if ($avatar == 'null') $sql = "UPDATE `user` set `last_login` = '$last_login' where `id`='$id'";
    else $sql = "UPDATE `user` set `last_login` = '$last_login', `avatar` = '$avatar' where `id`='$id'";
    $query = mysql_query($sql);
    print_r(json_encode('UserInfo has updated.'));
  }
  
?>